<template>
  <section class="jumbotron">
    <h3 class="jumbotron-heading">Search Github Users</h3>
    <div>
      <el-input
        type="text"
        placeholder="enter the name you search"
        v-model="keyWord"
        style="width: 150px"
      />&nbsp;
      <el-button round type="primary" icon="el-icon-search" @click="searchUsers"
        >搜索</el-button
      >
      <el-button type="warning" round @click="toPath">回到首页</el-button>
    </div>
  </section>
</template>

<script>
export default {
  name: "Search",
  data() {
    return {
      keyWord: "",
    };
  },
  methods: {
    searchUsers() {
      //请求前更新List
      this.$bus.$emit("updateListData", {
        isFirst: false,
        isLoading: true,
        errMsg: "",
        users: [],
      });

      //请求数据
      this.$http
        .get(`https://api.github.com/search/users?q=${this.keyWord}`)
        .then(
          (response) => {
            console.log("请求成功的数据", response.data); //所有数据
            //提供请求成功后的List数据
            this.$bus.$emit("updateListData", {
              isLoading: false,
              errMsg: "",
              users: response.data.items,
            });
            this.$message({
              message: "搜索成功",
              type: "success",
            });
          },
          (error) => {
            console.log("请求失败", error.message);
            //提供请求失败后的List数据
            this.$bus.$emit("updateListData", {
              isLoading: false,
              errMsg: error.message,
              users: [],
            });
            this.$message.error("搜索失败");
          }
        );
    },

    //跳转首页
    toPath() {
      this.$router.push("/home");
      this.$message({
        message: "成功回到首页",
        type: "success",
      });
    },
  },
};
</script>

<style></style>
